
#include "variable.h"
uint8_t amemory[MEMORY_SIZE] = { 0 };

uint8_t set_uint8(uint16_t addr, uint8_t val)
{
	amemory[addr] = val;
	return 0;
}

uint8_t get_uint8(uint16_t addr)
{
	return amemory[addr];
}

uint8_t set_int8(uint16_t addr, int8_t val)
{
	amemory[addr] = val;
	return 0;
}

int8_t get_int8(uint16_t addr)
{
	return (int8_t)(amemory[addr]);
}

uint8_t set_uint16(uint16_t addr, uint16_t val)
{
	uint16_t value = val;
	amemory[addr + 1] = value >> 8;
	amemory[addr + 0] = value;
	return 0;
}

uint16_t get_uint16(uint16_t addr)
{
	uint16_t value = 0;
	value = amemory[addr + 1];
	value = value << 8;
	value |= amemory[addr];
	return (value);
}

uint8_t set_int16(uint16_t addr, int16_t val)
{
	uint16_t value = val;
	amemory[addr + 1] = value >> 8;
	amemory[addr + 0] = value;
	return 0;
}

int16_t get_int16(uint16_t addr)
{
	uint16_t value = 0;
	value = amemory[addr + 1];
	value = value << 8;
	value |= amemory[addr];
	return (int16_t)(value);
}

uint8_t set_uint32(uint16_t addr, uint32_t val)
{
	uint32_t value = val;
	amemory[addr + 0] = value;
	value = value >> 8;
	amemory[addr + 1] = value;
	value = value >> 8;
	amemory[addr + 2] = value;
	value = value >> 8;
	amemory[addr + 3] = value;

	return 0;
}

uint32_t get_uint32(uint16_t addr)
{
	uint32_t value = 0;
	value = amemory[addr + 3];
	value = value << 8;
	value |= amemory[addr + 2];
	value = value << 8;
	value |= amemory[addr + 1];
	value = value << 8;
	value |= amemory[addr + 0];
	return (uint32_t)(value);
}

uint8_t set_int32(uint16_t addr, int32_t val)
{
	uint32_t value = val;
	amemory[addr + 0] = value;
	value = value >> 8;
	amemory[addr + 1] = value;
	value = value >> 8;
	amemory[addr + 2] = value;
	value = value >> 8;
	amemory[addr + 3] = value;

	return 0;
}

int32_t get_int32(uint16_t addr)
{
	uint32_t value = 0;
	value = amemory[addr + 3];
	value = value << 8;
	value |= amemory[addr + 2];
	value = value << 8;
	value |= amemory[addr + 1];
	value = value << 8;
	value |= amemory[addr + 0];
	return (int32_t)(value);
}

//uint8_t set_float(uint16_t addr,float val)
//{
//
//}
//float get_float(uint16_t addr);

uint8_t set_uint64(uint16_t addr, uint64_t val)
{
	uint64_t value = val;
	amemory[addr + 0] = value;
	value = value >> 8;
	amemory[addr + 1] = value;
	value = value >> 8;
	amemory[addr + 2] = value;
	value = value >> 8;
	amemory[addr + 3] = value;
	value = value >> 8;
	amemory[addr + 4] = value;
	value = value >> 8;
	amemory[addr + 5] = value;
	value = value >> 8;
	amemory[addr + 6] = value;
	value = value >> 8;
	amemory[addr + 7] = value;

	return 0;
}

uint64_t get_uint64(uint16_t addr)
{
	uint64_t value = 0;
	value = amemory[addr + 7];
	value = value << 8;
	value |= amemory[addr + 6];
	value = value << 8;
	value |= amemory[addr + 5];
	value = value << 8;
	value |= amemory[addr + 4];
	value = value << 8;
	value |= amemory[addr + 3];
	value = value << 8;
	value |= amemory[addr + 2];
	value = value << 8;
	value |= amemory[addr + 1];
	value = value << 8;
	value |= amemory[addr + 0];
	return (uint64_t)(value);
}

uint8_t set_int64(uint16_t addr, int64_t val)
{
	uint64_t value = val;
	amemory[addr + 0] = value;
	value = value >> 8;
	amemory[addr + 1] = value;
	value = value >> 8;
	amemory[addr + 2] = value;
	value = value >> 8;
	amemory[addr + 3] = value;
	value = value >> 8;
	amemory[addr + 4] = value;
	value = value >> 8;
	amemory[addr + 5] = value;
	value = value >> 8;
	amemory[addr + 6] = value;
	value = value >> 8;
	amemory[addr + 7] = value;

	return 0;
}

int64_t get_int64(uint16_t addr)
{
	uint64_t value = 0;
	value = amemory[addr + 7];
	value = value << 8;
	value |= amemory[addr + 6];
	value = value << 8;
	value |= amemory[addr + 5];
	value = value << 8;
	value |= amemory[addr + 4];
	value = value << 8;
	value |= amemory[addr + 3];
	value = value << 8;
	value |= amemory[addr + 2];
	value = value << 8;
	value |= amemory[addr + 1];
	value = value << 8;
	value |= amemory[addr + 0];
	return (int64_t)(value);
}
#include <string.h>
uint8_t set_type(variable_t *tmp,char *string)
{
	if (strcmp("int8",string) == 0)
	{
		tmp->type = VAR_NUMBER_INT8;
	}
	else if (strcmp("uint8",string) == 0)
	{
		tmp->type = VAR_NUMBER_UINT8;
	}
	else if (strcmp("int16",string) == 0)
	{
		tmp->type = VAR_NUMBER_INT16;
	}
	else if (strcmp("uint16",string) == 0)
	{
		tmp->type = VAR_NUMBER_UINT16;
	}
	else if (strcmp("int32",string) == 0)
	{
		tmp->type = VAR_NUMBER_INT32;
	}
	else if (strcmp("uint32",string) == 0)
	{
		tmp->type = VAR_NUMBER_UINT32;
	}
	else if (strcmp("int64",string) == 0)
	{
		tmp->type = VAR_NUMBER_INT64;
	}
	else if (strcmp("uint64",string) == 0)
	{
		tmp->type = VAR_NUMBER_UINT64;
	}
	else
	{
		return 1;
	}

	return 0;
}

uint8_t get_number(variable_t *tmp,uint32_t *value)
{
    uint8_t flag = 0;
	switch (tmp->type)
	{
	case VAR_NUMBER_UINT8:
		*value = get_uint8(tmp->addr);
		break;
	case VAR_NUMBER_INT8:
		*value = get_int8(tmp->addr);
		break;
	case VAR_NUMBER_UINT16:
		*value = get_uint16(tmp->addr);
		break;
	case VAR_NUMBER_INT16:
		*value = get_int16(tmp->addr);
		break;
	case VAR_NUMBER_UINT32:
		*value = get_uint32(tmp->addr);
		break;
	case VAR_NUMBER_INT32:
		*value = get_int32(tmp->addr);
		break;
	default:
		flag = 1;
		break;
	}

	return flag;
}

